/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Set;
import weibo4j.Weibo;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public final class cLeaderRank {

    public  static void cLeaderrank(DataPreparation data) throws FileNotFoundException, IOException {
        
        int KoutCount = 0;
        int KinCount = 0;
        for (String s : data.data.nodes) {
            KoutCount += data.Kout.get(s);
            String linksin[] = data.LinkIn.get(s).split(" ");
            KinCount += linksin.length;
        }

        /*
        System.out.println("==========S==========");
        for(String s:data.data.nodes){
        System.out.println(s+" "+data.S.get(s));
        }

        System.out.println("==========Kout==========");
        for(String s:data.data.nodes){
        System.out.println(s+" "+data.Kout.get(s));
        }
        System.out.println("==========linkin==========");
        for(String s:data.data.nodes){
        System.out.println(s+" "+data.LinkIn.get(s));
        }
        
        int KoutCount=0;
        int KinCount=0;
        for (String s :data.data.nodes) {
        KoutCount+=data.Kout.get(s);
        String linksin[]=data.LinkIn.get(s).split(" ");
        KinCount+=linksin.length;
        }

        System.out.println(KoutCount+" "+KinCount);
         *

        System.out.println("====================");
         */
        calS(data);
        saveData(data, data.path);
    }

    public static void calS(DataPreparation data) {
        int nodesCount = data.data.nodes.size();
        double cc = 0.0;//check convergence;
        Set keys = data.S.keySet();
        double newS[] = new double[nodesCount];
        for (int t = 1; t < 1001; t++) {
            double count = 0;
            //System.out.print(" Calculation time " + t + ": ");
            cc = 0;
            for (int i = 0; i < nodesCount; i++) {
                String key = (String) keys.toArray()[i];
                double snew = 0;
                double sold = data.S.get(key);
                String links = data.LinkIn.get(key);
                String linkin[] = links.split(" ");
                for (String keyfrom : linkin) {
                    double s = data.S.get(keyfrom);
                    int k = data.Kout.get(keyfrom);
                    snew += s / k;
                }
                newS[i] = snew;
                cc += Math.pow(snew - sold, 2);
                count += snew;
            }
            for (int i = 0; i < nodesCount; i++) {
                String key = (String) keys.toArray()[i];
                data.S.put(key, newS[i]);
            }
            //System.out.print(" count is: " + count + "\r\n");
            if (Math.sqrt(cc) < 0.0001) {
                
                break;
            }
        }
        double s0 = data.S.get("0") / (nodesCount - 1);
        for (String s : data.data.nodes) {
            if (!s.equals("0")) {
                double score = data.S.get(s);
                data.S.put(s, score + s0);
            }
        }
    }

    private static void saveData(DataPreparation data, String filename) throws FileNotFoundException, IOException {
        BufferedWriter scorewriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + "scores.txt")));
        BufferedWriter koutwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + "kout.txt")));
        BufferedWriter kinwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + "kin.txt")));
        String y="0";
        for (String s : data.data.nodes) {
            if (!s.equals("0")) {
                if (data.data.mBiFIDs.contains(s)){
                    y="1";
                }else{y="0";}
                scorewriter.write(data.data.username.get(s)+" "+data.S.get(s)+"\r\n");
                koutwriter.write(data.data.username.get(s) + " " + data.Kout.get(s) + "\r\n");
                kinwriter.write(data.data.username.get(s) + " " + data.Kin.get(s) + "\r\n");
            }
        }
        scorewriter.close();
        koutwriter.close();
        kinwriter.close();
    }
}
